package cn.com.ddapp.ddm.util;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;

import cn.com.ddapp.ddm.R;


/**
 * 加载框工具类
 * 
 * @author Administrator
 * 
 */
public class DialogUtils implements OnClickListener {
	private OnCancelListener cancelListener;
	private Dialog customDialog;
	private Context context;

	public DialogUtils(Context context) {
		this.context = context;
	}

	/**
	 * 构造loading 加载框
	 * 
	 * @param text
	 */
	public void getLoadingDialog(String text) {
		// 改变dialog的背景
		customDialog = new Dialog(context, R.style.progressDialog_Theme);
		customDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

		View view = LayoutInflater.from(context).inflate(
				R.layout.dialog_loading, null);
		if (text != null) {
			TextView textView = (TextView) view.findViewById(R.id.text);
			textView.setText(text);
		}
		customDialog.setContentView(view);
		// dialog = new AlertDialog.Builder(context).create();
		// // dialog.setTitle("加载中");
		// // dialog.setMessage("loading...");
		// // 給dialog设背景
		// View view =
		// LayoutInflater.from(context).inflate(R.layout.dialog_item,
		// null);
		// dialog.setView(view);
		// WindowManager.LayoutParams wlp = dialog.getWindow().getAttributes();
		// dialog.getWindow().setBackgroundDrawableResource(R.color.transparent);
		// dialog.getWindow().setAttributes(wlp);
		customDialog.setCancelable(true); // 设置不能回退消失
		customDialog.show();
	}

	// /**
	// * 构造警示框
	// *
	// * @param text
	// */
	// public void getCustomDialog(int theme, int resLayout, String title,
	// String content, String btn1, String btn2) {
	// // 改变dialog的背景
	// customDialog = new Dialog(context, theme);
	// // customDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
	// View view = LayoutInflater.from(context).inflate(resLayout, null);
	// TextView textTitle = (TextView) view.findViewById(R.id.title);
	// TextView textContent = (TextView) view.findViewById(R.id.content);
	// Button cancel_btn = (Button) view.findViewById(R.id.cancel_btn);
	// Button confirm_btn = (Button) view.findViewById(R.id.confirm_btn);
	// cancel_btn.setOnClickListener(this);
	// confirm_btn.setOnClickListener(this);
	//
	// textTitle.setText(title);
	// textContent.setText(content);
	// cancel_btn.setText(content);
	// confirm_btn.setText(content);
	//
	// customDialog.setContentView(view);
	// customDialog.setCancelable(true); // 设置不能回退消失
	// customDialog.show();
	// }

	public void closeDialog() {
		if(customDialog!=null){
			if (customDialog.isShowing()) {
				customDialog.dismiss();
			}
		}

	}

	public void setOnCancelListener(OnCancelListener cancelListener) {
		if (customDialog!=null) {
			customDialog.setOnCancelListener(cancelListener);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

	public void showAlertDialog(Context context, String title, String message,
			boolean isAler, boolean cancelable,DialogInterface.OnClickListener onOkClickListener,
			DialogInterface.OnClickListener onQxClickListener) {

		CustomDialog.Builder builder = new CustomDialog.Builder(context,
				R.style.customDailogStyle, R.layout.custom_dialog,cancelable);
		builder.setMessage(message);
		builder.setTitle(title);
		builder.setPositiveButton("确定", onOkClickListener);
		if (isAler) {
			builder.setNegativeButton(null, onQxClickListener);
		} else {
			builder.setNegativeButton("取消", onQxClickListener);
		}

		builder.create().show();

	}
	
	public void showAlertDialogForUpdate(Context context, String title, String message,
			boolean isAler,boolean cancelable,String btn1,String btn2, DialogInterface.OnClickListener onOkClickListener,
			DialogInterface.OnClickListener onQxClickListener) {

		CustomDialog.Builder builder = new CustomDialog.Builder(context,
				R.style.customDailogStyle, R.layout.custom_dialog,cancelable);
		builder.setMessage(message);
		builder.setTitle(title);
		builder.setPositiveButton(btn1, onOkClickListener);
		if (isAler) {
			builder.setNegativeButton(null, onQxClickListener);
		} else {
			builder.setNegativeButton(btn2, onQxClickListener);
		}

		builder.create().show();

	}
}
